package com.bright.ghj.overt.web.controller;

import com.bright.ghj.common.pojo.bo.RestResult;
import com.bright.ghj.overt.util.SecurityUtil;
import com.bright.ghj.overt.constant.DataConstant;
import com.bright.ghj.overt.constant.PublicConstant;
import com.bright.ghj.overt.pojo.vo.NavigateVO;
import com.bright.ghj.overt.pojo.vo.TableTypeVO;
import com.bright.ghj.overt.pojo.vo.WebMenuVO;
import com.bright.ghj.overt.service.NavigateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author hxj
 * @Date 2023/12/21 11:05
 * @Description 导航项
 */
@RequestMapping("/navigate")
@RestController
@RequiredArgsConstructor
@Api(tags = "导航接口")
public class NavigateController {

    private final NavigateService navigateService;

    private final DataConstant dataConstant;

    @Autowired
    private CacheManager cacheManager;


    /**
     * 获取当前用户有权限查看的导航
     * @return 分一级导航 二级导航
     */
    @ApiOperation(value = "获取导航列表")
    @GetMapping("/list")
    public RestResult listAllNavigates() {
//        String userName = "超级用户"; // TODO
        String userName = getLoginUserName();
        List<NavigateVO> navigateVOS = navigateService.listAllNavigates(userName);
        return RestResult.success(navigateVOS);
    }

    /**
     * 获取当前用户有权限查看的所有表类型
     * @return 一级：菜单分类列表  二级：表类型列表
     */
    @ApiOperation(value = "获取公开表类型")
    @GetMapping("/tableType/list")
    public RestResult listAllTableTypes() {
        List<WebMenuVO> webMenuList = navigateService.listAllWebMenus();
        // 测试
        for (WebMenuVO webMenuVO : webMenuList) {
            if (webMenuVO.getMenuId().equals("06")) {
                webMenuVO.setMenuId("04");
            }
        }
        return RestResult.success(webMenuList);
    }

    /**
     * 获取当前用户有权限查看的所有表类型
     * @return 一级：菜单分类列表  二级：表类型列表
     */
    @ApiOperation(value = "按类型获取公开表类型")
    @GetMapping("/tableType/listByYwType")
    public RestResult listAllTableTypesByYwType(String ywType) {
        List<TableTypeVO> tableTypeVOS = getTableTypes(ywType == null ? null : Integer.parseInt(ywType));
        return RestResult.success(tableTypeVOS);
    }

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/userInfo")
    public RestResult getUserInfo() {
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//
//        SecurityUser securityUser = (SecurityUser)principal;
//        User loginUser = securityUser.getUser();
//        loginUser.setPassword(null);

//        return RestResult.success(navigateService.getLoginUser());
        return RestResult.success(SecurityUtil.getLoginUser());
    }

    /**
     * 清除缓存
     * @return
     */
    @ApiOperation(value = "清除所有缓存")
    @GetMapping("/clearCache")
    public RestResult clearCache(){
        cacheManager
                .getCacheNames()
                .stream()
                .forEach(cacheName -> cacheManager.getCache(cacheName).clear());
        return RestResult.success();
    }

    private List<TableTypeVO> getTableTypes(Integer ywType) {
        String category;
        if (ywType == PublicConstant.GK_TYPE_DW) {
            category = dataConstant.DW_CATEGORY;
        } else if (ywType == PublicConstant.GK_TYPE_SW) {
            category = dataConstant.SW_CATEGORY;
        } else {
            category = dataConstant.CW_CATEGORY;
        }
        return navigateService.listTableTypesByMenuId(category);
    }

    private String getLoginUserName() {
        return SecurityUtil.getLoginUser().getUsername();
    }



}
